cmake_minimum_required(VERSION 3.16)
project(webrtc-2.1-demo)

set(CMAKE_CXX_STANDARD 17)
add_definitions(-DWEBRTC_LINUX -DWEBRTC_POSIX -DWAP_DISABLE_INLINE_SSE)

add_compile_options("-Wno-return-type")

set(PATH ${PROJECT_SOURCE_DIR}/webrtc-audio-processing-2.1/webrtc)

include_directories(${PATH} ${PROJECT_SOURCE_DIR}/webrtc-audio-processing-2.1/subprojects/abseil-cpp-20240722.1)

set(common_audio_
    ${PATH}/common_audio/audio_converter.cc
    ${PATH}/common_audio/audio_util.cc
    ${PATH}/common_audio/channel_buffer.cc
    ${PATH}/common_audio/fir_filter_c.cc
    ${PATH}/common_audio/fir_filter_factory.cc
    ${PATH}/common_audio/resampler/push_resampler.cc
    ${PATH}/common_audio/resampler/push_sinc_resampler.cc
    ${PATH}/common_audio/resampler/resampler.cc
    ${PATH}/common_audio/resampler/sinc_resampler.cc
    ${PATH}/common_audio/resampler/sinusoidal_linear_chirp_source.cc
    ${PATH}/common_audio/ring_buffer.c
    ${PATH}/common_audio/signal_processing/auto_correlation.c
    ${PATH}/common_audio/signal_processing/auto_corr_to_refl_coef.c
    ${PATH}/common_audio/signal_processing/complex_bit_reverse.c
    ${PATH}/common_audio/signal_processing/complex_fft.c
    ${PATH}/common_audio/signal_processing/copy_set_operations.c
    ${PATH}/common_audio/signal_processing/cross_correlation.c
    ${PATH}/common_audio/signal_processing/division_operations.c
    ${PATH}/common_audio/signal_processing/dot_product_with_scale.cc
    ${PATH}/common_audio/signal_processing/downsample_fast.c
    ${PATH}/common_audio/signal_processing/filter_ar.c
    ${PATH}/common_audio/signal_processing/filter_ar_fast_q12.c
    ${PATH}/common_audio/signal_processing/energy.c
    ${PATH}/common_audio/signal_processing/filter_ma_fast_q12.c
    ${PATH}/common_audio/signal_processing/get_hanning_window.c
    ${PATH}/common_audio/signal_processing/get_scaling_square.c
    ${PATH}/common_audio/signal_processing/ilbc_specific_functions.c
    ${PATH}/common_audio/signal_processing/levinson_durbin.c
    ${PATH}/common_audio/signal_processing/lpc_to_refl_coef.c
    ${PATH}/common_audio/signal_processing/min_max_operations.c
    ${PATH}/common_audio/signal_processing/randomization_functions.c
    ${PATH}/common_audio/signal_processing/real_fft.c
    ${PATH}/common_audio/signal_processing/refl_coef_to_lpc.c
    ${PATH}/common_audio/signal_processing/resample_48khz.c
    ${PATH}/common_audio/signal_processing/resample_by_2.c
    ${PATH}/common_audio/signal_processing/resample_by_2_internal.c
    ${PATH}/common_audio/signal_processing/resample.c
    ${PATH}/common_audio/signal_processing/resample_fractional.c
    ${PATH}/common_audio/signal_processing/spl_init.c
    ${PATH}/common_audio/signal_processing/spl_inl.c
    ${PATH}/common_audio/signal_processing/splitting_filter.c
    ${PATH}/common_audio/signal_processing/spl_sqrt.c
    ${PATH}/common_audio/signal_processing/sqrt_of_one_minus_x_squared.c
    ${PATH}/common_audio/signal_processing/vector_scaling_operations.c
    ${PATH}/common_audio/smoothing_filter.cc
    ${PATH}/common_audio/third_party/ooura/fft_size_128/ooura_fft.cc
    ${PATH}/common_audio/third_party/ooura/fft_size_256/fft4g.cc
    ${PATH}/common_audio/third_party/spl_sqrt_floor/spl_sqrt_floor.c
    ${PATH}/common_audio/vad/vad.cc
    ${PATH}/common_audio/vad/vad_core.c
    ${PATH}/common_audio/vad/vad_filterbank.c
    ${PATH}/common_audio/vad/vad_gmm.c
    ${PATH}/common_audio/vad/vad_sp.c
    ${PATH}/common_audio/vad/webrtc_vad.c
)

set(common_audio_sse2_    
    ${PATH}/common_audio/fir_filter_sse.cc  
    ${PATH}/common_audio/resampler/sinc_resampler_sse.cc
    ${PATH}/common_audio/third_party/ooura/fft_size_128/ooura_fft_sse2.cc
)
add_library(common_audio_sse2 STATIC ${common_audio_sse2_})
target_compile_options(common_audio_sse2 PRIVATE -msse2)

set(rtc_base_
    ${PATH}/rtc_base/checks.cc
    ${PATH}/rtc_base/containers/flat_tree.cc
    ${PATH}/rtc_base/event.cc
    ${PATH}/rtc_base/event_tracer.cc
    ${PATH}/rtc_base/experiments/field_trial_parser.cc
    ${PATH}/rtc_base/logging.cc
    ${PATH}/rtc_base/memory/aligned_malloc.cc
    ${PATH}/rtc_base/platform_thread.cc
    ${PATH}/rtc_base/platform_thread_types.cc
    ${PATH}/rtc_base/race_checker.cc
    ${PATH}/rtc_base/random.cc
    ${PATH}/rtc_base/string_encode.cc
    ${PATH}/rtc_base/string_to_number.cc
    ${PATH}/rtc_base/string_utils.cc
    ${PATH}/rtc_base/strings/string_builder.cc
    ${PATH}/rtc_base/synchronization/sequence_checker_internal.cc
    ${PATH}/rtc_base/synchronization/yield_policy.cc
    ${PATH}/rtc_base/system/file_wrapper.cc
    ${PATH}/rtc_base/system_time.cc
    ${PATH}/rtc_base/time_utils.cc
    ${PATH}/rtc_base/zero_memory.cc
)

set(system_wrappers_
    ${PATH}/system_wrappers/source/cpu_features.cc
    ${PATH}/system_wrappers/source/denormal_disabler.cc
    ${PATH}/system_wrappers/source/field_trial.cc
    ${PATH}/system_wrappers/source/metrics.cc
    ${PATH}/system_wrappers/source/sleep.cc
)

set(api_
    ${PATH}/api/audio/audio_frame.cc
    ${PATH}/api/audio/audio_processing.cc
    ${PATH}/api/audio/audio_processing_statistics.cc
    ${PATH}/api/audio/channel_layout.cc
    ${PATH}/api/audio/echo_canceller3_config.cc
    ${PATH}/api/rtp_headers.cc
    ${PATH}/api/rtp_packet_info.cc
    ${PATH}/api/task_queue/task_queue_base.cc
    ${PATH}/api/units/frequency.cc
    ${PATH}/api/units/time_delta.cc
    ${PATH}/api/units/timestamp.cc
    ${PATH}/api/video/color_space.cc
    ${PATH}/api/video/hdr_metadata.cc
    ${PATH}/api/video/video_content_type.cc
    ${PATH}/api/video/video_timing.cc
)

add_definitions(-DWEBRTC_APM_DEBUG_DUMP=0)
set(audio_processing_aec3_
    ${PATH}/modules/audio_processing/aec_dump/null_aec_dump_factory.cc
    ${PATH}/modules/audio_processing/aec3/adaptive_fir_filter.cc
    ${PATH}/modules/audio_processing/aec3/adaptive_fir_filter_erl.cc
    ${PATH}/modules/audio_processing/aec3/aec3_common.cc
    ${PATH}/modules/audio_processing/aec3/aec3_fft.cc
    ${PATH}/modules/audio_processing/aec3/aec_state.cc
    ${PATH}/modules/audio_processing/aec3/alignment_mixer.cc
    ${PATH}/modules/audio_processing/aec3/api_call_jitter_metrics.cc
    ${PATH}/modules/audio_processing/aec3/block_buffer.cc
    ${PATH}/modules/audio_processing/aec3/block_delay_buffer.cc
    ${PATH}/modules/audio_processing/aec3/block_framer.cc
    ${PATH}/modules/audio_processing/aec3/block_processor.cc
    ${PATH}/modules/audio_processing/aec3/block_processor_metrics.cc
    ${PATH}/modules/audio_processing/aec3/clockdrift_detector.cc
    ${PATH}/modules/audio_processing/aec3/coarse_filter_update_gain.cc
    ${PATH}/modules/audio_processing/aec3/comfort_noise_generator.cc
    ${PATH}/modules/audio_processing/aec3/config_selector.cc
    ${PATH}/modules/audio_processing/aec3/decimator.cc
    ${PATH}/modules/audio_processing/aec3/dominant_nearend_detector.cc
    ${PATH}/modules/audio_processing/aec3/downsampled_render_buffer.cc
    ${PATH}/modules/audio_processing/aec3/echo_audibility.cc
    ${PATH}/modules/audio_processing/aec3/echo_canceller3.cc
    ${PATH}/modules/audio_processing/aec3/echo_path_delay_estimator.cc
    ${PATH}/modules/audio_processing/aec3/echo_path_variability.cc
    ${PATH}/modules/audio_processing/aec3/echo_remover.cc
    ${PATH}/modules/audio_processing/aec3/echo_remover_metrics.cc
    ${PATH}/modules/audio_processing/aec3/erle_estimator.cc
    ${PATH}/modules/audio_processing/aec3/erl_estimator.cc
    ${PATH}/modules/audio_processing/aec3/fft_buffer.cc
    ${PATH}/modules/audio_processing/aec3/filter_analyzer.cc
    ${PATH}/modules/audio_processing/aec3/frame_blocker.cc
    ${PATH}/modules/audio_processing/aec3/fullband_erle_estimator.cc
    ${PATH}/modules/audio_processing/aec3/matched_filter.cc
    ${PATH}/modules/audio_processing/aec3/matched_filter_lag_aggregator.cc
    ${PATH}/modules/audio_processing/aec3/moving_average.cc
    ${PATH}/modules/audio_processing/aec3/multi_channel_content_detector.cc
    ${PATH}/modules/audio_processing/aec3/refined_filter_update_gain.cc
    ${PATH}/modules/audio_processing/aec3/render_buffer.cc
    ${PATH}/modules/audio_processing/aec3/render_delay_buffer.cc
    ${PATH}/modules/audio_processing/aec3/render_delay_controller.cc
    ${PATH}/modules/audio_processing/aec3/render_delay_controller_metrics.cc
    ${PATH}/modules/audio_processing/aec3/render_signal_analyzer.cc
    ${PATH}/modules/audio_processing/aec3/residual_echo_estimator.cc
    ${PATH}/modules/audio_processing/aec3/reverb_decay_estimator.cc
    ${PATH}/modules/audio_processing/aec3/reverb_frequency_response.cc
    ${PATH}/modules/audio_processing/aec3/reverb_model.cc
    ${PATH}/modules/audio_processing/aec3/reverb_model_estimator.cc
    ${PATH}/modules/audio_processing/aec3/signal_dependent_erle_estimator.cc
    ${PATH}/modules/audio_processing/aec3/spectrum_buffer.cc
    ${PATH}/modules/audio_processing/aec3/stationarity_estimator.cc
    ${PATH}/modules/audio_processing/aec3/subband_erle_estimator.cc
    ${PATH}/modules/audio_processing/aec3/subband_nearend_detector.cc
    ${PATH}/modules/audio_processing/aec3/subtractor.cc
    ${PATH}/modules/audio_processing/aec3/subtractor_output_analyzer.cc
    ${PATH}/modules/audio_processing/aec3/subtractor_output.cc
    ${PATH}/modules/audio_processing/aec3/suppression_filter.cc
    ${PATH}/modules/audio_processing/aec3/suppression_gain.cc
    ${PATH}/modules/audio_processing/aec3/transparent_mode.cc
)

set(audio_processing_aecm_
    ${PATH}/modules/audio_processing/aecm/aecm_core.cc
    ${PATH}/modules/audio_processing/aecm/echo_control_mobile.cc
    ${PATH}/modules/audio_processing/aecm/aecm_core_c.cc
    ${PATH}/modules/audio_processing/utility/cascaded_biquad_filter.cc
    ${PATH}/modules/audio_processing/utility/delay_estimator.cc
    ${PATH}/modules/audio_processing/utility/delay_estimator_wrapper.cc    
)

set(agc2_
    ${PATH}/modules/audio_processing/agc2/adaptive_digital_gain_controller.cc
    ${PATH}/modules/audio_processing/agc2/agc2_testing_common.cc
    ${PATH}/modules/audio_processing/agc2/biquad_filter.cc
    ${PATH}/modules/audio_processing/agc2/clipping_predictor.cc
    ${PATH}/modules/audio_processing/agc2/clipping_predictor_level_buffer.cc
    ${PATH}/modules/audio_processing/agc2/compute_interpolated_gain_curve.cc
    ${PATH}/modules/audio_processing/agc2/cpu_features.cc
    ${PATH}/modules/audio_processing/agc2/fixed_digital_level_estimator.cc
    ${PATH}/modules/audio_processing/agc2/gain_applier.cc
    ${PATH}/modules/audio_processing/agc2/input_volume_controller.cc
    ${PATH}/modules/audio_processing/agc2/input_volume_stats_reporter.cc
    ${PATH}/modules/audio_processing/agc2/interpolated_gain_curve.cc
    ${PATH}/modules/audio_processing/agc2/limiter.cc
    ${PATH}/modules/audio_processing/agc2/limiter_db_gain_curve.cc
    ${PATH}/modules/audio_processing/agc2/noise_level_estimator.cc
    ${PATH}/modules/audio_processing/agc2/rnn_vad/auto_correlation.cc
    ${PATH}/modules/audio_processing/agc2/rnn_vad/features_extraction.cc
    ${PATH}/modules/audio_processing/agc2/rnn_vad/lp_residual.cc
    ${PATH}/modules/audio_processing/agc2/rnn_vad/pitch_search.cc
    ${PATH}/modules/audio_processing/agc2/rnn_vad/pitch_search_internal.cc
    ${PATH}/modules/audio_processing/agc2/rnn_vad/rnn.cc
    ${PATH}/modules/audio_processing/agc2/rnn_vad/rnn_fc.cc
    ${PATH}/modules/audio_processing/agc2/rnn_vad/rnn_gru.cc
    ${PATH}/modules/audio_processing/agc2/rnn_vad/spectral_features.cc
    ${PATH}/modules/audio_processing/agc2/rnn_vad/spectral_features_internal.cc
    ${PATH}/modules/audio_processing/agc2/saturation_protector.cc
    ${PATH}/modules/audio_processing/agc2/saturation_protector_buffer.cc
    ${PATH}/modules/audio_processing/agc2/speech_level_estimator.cc
    ${PATH}/modules/audio_processing/agc2/speech_probability_buffer.cc
    ${PATH}/modules/audio_processing/agc2/vad_wrapper.cc
    ${PATH}/modules/audio_processing/agc2/vector_float_frame.cc
)

set(third_party_
    ${PATH}/modules/audio_processing/utility/pffft_wrapper.cc
    ${PATH}/third_party/pffft/src/pffft.c
    ${PATH}/third_party/rnnoise/src/rnn_vad_weights.cc
    ${PATH}/common_audio/third_party/ooura/fft_size_128/ooura_fft.cc
    ${PATH}/common_audio/third_party/ooura/fft_size_256/fft4g.cc
    ${PATH}/common_audio/third_party/spl_sqrt_floor/spl_sqrt_floor.c
)

set(x86_
    ${PATH}/modules/audio_processing/aec3/adaptive_fir_filter_avx2.cc
    ${PATH}/modules/audio_processing/aec3/adaptive_fir_filter_erl_avx2.cc
    ${PATH}/modules/audio_processing/aec3/fft_data_avx2.cc
    ${PATH}/modules/audio_processing/aec3/matched_filter_avx2.cc
    ${PATH}/modules/audio_processing/aec3/vector_math_avx2.cc
    ${PATH}/modules/audio_processing/agc2/rnn_vad/vector_math_avx2.cc    

)
add_library(webrtc_audio_processing_privatearch STATIC ${x86_})
target_compile_options(webrtc_audio_processing_privatearch PRIVATE -mavx2 -mfma)

set(ns_
    ${PATH}/modules/audio_processing/ns/fast_math.cc
    ${PATH}/modules/audio_processing/ns/histograms.cc
    ${PATH}/modules/audio_processing/ns/noise_estimator.cc
    ${PATH}/modules/audio_processing/ns/noise_suppressor.cc
    ${PATH}/modules/audio_processing/ns/ns_fft.cc
    ${PATH}/modules/audio_processing/ns/prior_signal_model.cc
    ${PATH}/modules/audio_processing/ns/prior_signal_model_estimator.cc
    ${PATH}/modules/audio_processing/ns/quantile_noise_estimator.cc
    ${PATH}/modules/audio_processing/ns/signal_model.cc
    ${PATH}/modules/audio_processing/ns/signal_model_estimator.cc
    ${PATH}/modules/audio_processing/ns/speech_probability_estimator.cc
    ${PATH}/modules/audio_processing/ns/suppression_params.cc
    ${PATH}/modules/audio_processing/ns/wiener_filter.cc
)

set(vad_
    ${PATH}/modules/audio_processing/vad/gmm.cc
    ${PATH}/modules/audio_processing/vad/pitch_based_vad.cc
    ${PATH}/modules/audio_processing/vad/pitch_internal.cc
    ${PATH}/modules/audio_processing/vad/pole_zero_filter.cc
    ${PATH}/modules/audio_processing/vad/standalone_vad.cc
    ${PATH}/modules/audio_processing/vad/vad_audio_proc.cc
    ${PATH}/modules/audio_processing/vad/vad_circular_buffer.cc
    ${PATH}/modules/audio_processing/vad/voice_activity_detector.cc
)

set(logging_
    ${PATH}/modules/audio_processing/logging/apm_data_dumper.cc
)

set(modules_impl_
    ${PATH}/modules/audio_processing/high_pass_filter.cc
)

option(ABSL_PROPAGATE_CXX_STD "abseil cpp option" ON)
add_subdirectory(${PROJECT_SOURCE_DIR}/webrtc-audio-processing-2.1/subprojects/abseil-cpp-20240722.1)

add_library(webrtc_audio_processing STATIC
    ${common_audio_}
    ${rtc_base_}
    ${system_wrappers_}
    ${api_}
    ${audio_processing_aec3_}
    ${audio_processing_aecm_}
    ${agc2_}
    ${ns_}
    ${vad_}
    ${third_party_} 
    ${logging_}
    ${modules_impl_}
)
target_link_libraries(webrtc_audio_processing PUBLIC webrtc_audio_processing_privatearch common_audio_sse2)

add_executable(webrtctest ${PROJECT_SOURCE_DIR}/examples/test.cc)

target_link_libraries(webrtctest PUBLIC 
    webrtc_audio_processing 
    absl_base
    absl_flags
    absl_strings
    absl_numeric
    absl_synchronization
    absl_bad_optional_access
)
